package com.sitech.portal.system.action;

import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.form.SysStyleForm;
import com.sitech.portal.system.service.ISysStyleService;

public class SysStyleAction extends JGridBaseAction {
	
	private SysStyleForm theForm;
	private ISysStyleService sysStyleService;
	private List stylelist;
	private String flag;
	
	public SysStyleForm getTheForm() {
		return theForm;
	}

	public void setTheForm(SysStyleForm theForm) {
		this.theForm = theForm;
	}

	public ISysStyleService getSysStyleService() {
		return sysStyleService;
	}

	public void setSysStyleService(ISysStyleService sysStyleService) {
		this.sysStyleService = sysStyleService;
	}

	public List getStylelist() {
		return stylelist;
	}

	public void setStylelist(List stylelist) {
		this.stylelist = stylelist;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String loadStyleList() {
		IPage result = sysStyleService.loadSysStyleList(this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	/**
	 * 
	 * loadDefaultStyleList:(载入个性化皮肤页面)
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 * @throws
	 * @since CodingExample Ver 1.1
	 */
	public String loadDefaultStyleList() {
		stylelist = sysStyleService.getDefaultStyleList();
		return SUCCESS;

	}

	/**
	 * 
	 * saveDefaultstyle:(保存个性化中的皮肤)
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 * @throws
	 * @since CodingExample Ver 1.1
	 */
	public String saveDefaultstyle() {

		flag = "true";
		try{
			String STYLE_ID = this.getRequest().getParameter("styleid");
			if (STYLE_ID != null) {
				sysStyleService.clearDefaultForSysStyle();
				sysStyleService.updateDefaultStyle(STYLE_ID);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	/**
	 * 增加或编辑系统样式表信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */

	public String editSysStyle() {

		if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			return SUCCESS;
		}

		String[] style_ids = this.getRequest().getParameterValues("STYLE_ID");

		if (style_ids != null && style_ids[0] != null) {
			HashMap map = sysStyleService.findSysStyleByStyle_id(style_ids[0]);
			theForm.setENABLE(String.valueOf(map.get("ENABLE")));
			theForm.setSTYLE_NAME(String.valueOf(map.get("STYLE_NAME")));
			theForm.setIS_DEFAULT(String.valueOf(map.get("IS_DEFAULT")));
			theForm.setSTYLE_DESC(String.valueOf(map.get("STYLE_DESC")));
			theForm.setSTYLE_ID(String.valueOf(map.get("STYLE_ID")));
			theForm.setSTYLE_PATH(String.valueOf(map.get("STYLE_PATH")));
			theForm.setIS_TAB((String.valueOf(map.get("IS_TAB"))));
		}
		return SUCCESS;
	}

	public String saveSysStyle() {
		flag = "true";
		try{
			String style_id = "";
			HashMap map = new HashMap();
			if (theForm.getSTYLE_ID() != null && !"".equals(theForm.getSTYLE_ID())) {
				style_id = theForm.getSTYLE_ID();
			} else {
				style_id = sysStyleService.getNextSysStyleID();
			}
			map.put("STYLE_ID", style_id);
			map.put("STYLE_NAME", theForm.getSTYLE_NAME());
			map.put("STYLE_DESC", theForm.getSTYLE_DESC());
			map.put("STYLE_PATH", theForm.getSTYLE_PATH());
			map.put("ENABLE", theForm.getENABLE());
			map.put("IS_TAB", theForm.getIS_TAB());
			map.put("IS_DEFAULT", theForm.getIS_DEFAULT());
			if ("1".equals(theForm.getIS_DEFAULT())) {
				sysStyleService.clearDefaultForSysStyle();
				// 修改session中的stylePath
				TblSYS_LOGINMSG user = this.getCurrentUser();
				user.setStylePath(theForm.getSTYLE_PATH());
			}
			if (theForm.getSTYLE_ID() != null && !"".equals(theForm.getSTYLE_ID())) {
				sysStyleService.updateForSysStyle(map);
			} else {
				sysStyleService.insertForSysStyle(map);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	public String deleteSysStyle() {
		flag = "true";
		try{
			String[] style_ids = this.getRequest().getParameterValues("STYLE_ID");
			sysStyleService.deleteForSysStyle(style_ids);
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

}
